#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <bitset>
#include <QVector>
#include <des.h>

int times = 0;
int programBegin = 0;

using std::bitset;
using std::string;
using std::cout;
using std::endl;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    times = 0;
    programBegin = 1;
    mydes.rightTempkey.clear();
    mydes.leftTempKey.clear();

    QString dataStr = ui->dataEdit->text(); //明文
    QString keyStr = ui->keyEdit->text();  //密钥
    QString encry = ""; //密文

    //8字符明文转化成64bit二进制
    mydes.setPlain(mydes.charToBitset(dataStr.toLocal8Bit()));
    string bitPlain = mydes.getPlain().to_string();
    ui->dataEdit_bit->setText(QString::fromLocal8Bit(bitPlain.c_str()));

    //8字符密钥转化成64bit二进制
    mydes.setKey(mydes.charToBitset(keyStr.toLocal8Bit()));
    cout<<mydes.getKey()<<endl;
    string bitKey = mydes.getKey().to_string();
    ui->keyEdit_bit->setText(QString::fromLocal8Bit(bitKey.c_str()));

    //生成16个子密钥
    mydes.generateKeys();

    //加密
    mydes.encrypt();
    string bitCipher = mydes.getCipher().to_string();
    ui->encryEdit->setText(QString::fromLocal8Bit(bitCipher.c_str()));

}

void MainWindow::on_decryBtn_clicked()
{

    //生成16个子密钥
    mydes.generateKeys();

    //解密
    mydes.decrypt();
    string bitDeplain = mydes.getDeplain().to_string();
    ui->deplainEdit->setText(QString::fromLocal8Bit(bitDeplain.c_str()));
    cout<<bitDeplain<<endl;
}

void MainWindow::on_nextTimeBtn_clicked()
{
    if( programBegin == 1) {

        ui->timesEdit->setText(QString::number(times));

        if (times < 16) {

            //左侧
            bitset<32> flag = mydes.leftTempKey[times];
            ui->bitLeftTableWidget->clear();
            ui->bitRightTableWidget->clear();
            int num = 0;
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 8; j++) {
                    QTableWidgetItem *item = new QTableWidgetItem(QString::number(flag[num++]));
                    if ( times%2 == 1) {
                        item->setBackground(QBrush(QColor(255, 255, 200)));
                    } else {
                        item->setBackground(QBrush(QColor(255, 255, 255)));
                    }
                    ui->bitLeftTableWidget->setItem(i,j,item);
                }
            }
            //右侧
            num = 0;
            flag = mydes.rightTempkey[times];
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 8; j++) {
                    QTableWidgetItem *item = new QTableWidgetItem(QString::number(flag[num++]));
                    if ( times%2 == 1) {
                        item->setBackground(QBrush(QColor(255, 255, 255)));
                    } else {
                        item->setBackground(QBrush(QColor(255, 255, 200)));
                    }
                    ui->bitRightTableWidget->setItem(i,j,item);
                }
            }

            times += 1;
        } else {
            times = 0;
        }
    } else {
        //;
    }

}
